home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Tools / freeze / winmakemakefile.py < prev   
Encoding:
Python Source  |  2000-06-23  |  4.6 KB  |  145 lines

  1. import sys, os, string
  2.  
  3. # Template used then the program is a GUI program
  4. WINMAINTEMPLATE = """
  5. #include <windows.h>
  6.  
  7. int WINAPI WinMain(
  8.     HINSTANCE hInstance,      // handle to current instance
  9.     HINSTANCE hPrevInstance,  // handle to previous instance
  10.     LPSTR lpCmdLine,          // pointer to command line
  11.     int nCmdShow              // show state of window
  12.     )
  13. {
  14.     PyImport_FrozenModules = _PyImport_FrozenModules;
  15.     return Py_FrozenMain(__argc, __argv);
  16. }
  17. """
  18.  
  19. SERVICETEMPLATE = """
  20. extern int PythonService_main(int, char **);
  21.  
  22. int main( int argc, char **argv)
  23. {
  24.     PyImport_FrozenModules = _PyImport_FrozenModules;
  25.     return PythonService_main(argc, argv);
  26. }
  27. """
  28.  
  29. subsystem_details = {
  30.     # -s flag        : (C entry point template), (is it __main__?), (is it a DLL?)
  31.     'console'        : (None,                    1,                 0),
  32.     'windows'        : (WINMAINTEMPLATE,         1,                 0),
  33.     'service'        : (SERVICETEMPLATE,         0,                 0),
  34.     'com_dll'        : ("",                      0,                 1),
  35. }
  36.  
  37. def get_custom_entry_point(subsystem):
  38.     try:
  39.         return subsystem_details[subsystem][:2]
  40.     except KeyError:
  41.         raise ValueError, "The subsystem %s is not known" % subsystem
  42.  
  43.  
  44. def makemakefile(outfp, vars, files, target):
  45.     save = sys.stdout
  46.     try:
  47.         sys.stdout = outfp
  48.         realwork(vars, files, target)
  49.     finally:
  50.         sys.stdout = save
  51.  
  52. def realwork(vars, moddefns, target):
  53.     print "# Makefile for Microsoft Visual C++ generated by freeze.py script"
  54.     print
  55.     print 'target = %s' % target
  56.     print 'pythonhome = "%s"' % vars['prefix']
  57.     print
  58.     print 'DEBUG=0 # Set to 1 to use the _d versions of Python.'
  59.     print '!IF $(DEBUG)'
  60.     print 'debug_suffix=_d'
  61.     print 'c_debug=/Zi /Od /DDEBUG /D_DEBUG'
  62.     print 'l_debug=/DEBUG'
  63.     print 'temp_dir=Build\\Debug'
  64.     print '!ELSE'
  65.     print 'debug_suffix='
  66.     print 'c_debug=/Ox'
  67.     print 'l_debug='
  68.     print 'temp_dir=Build\\Release'
  69.     print '!ENDIF'
  70.     print
  71.  
  72.     print '# The following line assumes you have built Python using the standard instructions'
  73.     print '# Otherwise fix the following line to point to the library.'
  74.     print 'pythonlib = "$(pythonhome)/pcbuild/python15$(debug_suffix).lib"'
  75.     print
  76.  
  77.     # We only ever write one "entry point" symbol - either
  78.     # "main" or "WinMain".  Therefore, there is no need to
  79.     # pass a subsystem switch to the linker as it works it
  80.     # out all by itself.  However, the subsystem _does_ determine
  81.     # the file extension and additional linker flags.
  82.     target_link_flags = ""
  83.     target_ext = ".exe"
  84.     if subsystem_details[vars['subsystem']][2]:
  85.         target_link_flags = "-dll"
  86.         target_ext = ".dll"
  87.  
  88.  
  89.     print "# As the target uses Python15.dll, we must use this compiler option!"
  90.     print "cdl = /MD"
  91.     print
  92.     print "all: $(target)$(debug_suffix)%s" % (target_ext)
  93.     print
  94.  
  95.     print '$(temp_dir):'
  96.     print '  if not exist $(temp_dir)\. mkdir $(temp_dir)'
  97.     print
  98.  
  99.     objects = []
  100.     libs = ["shell32.lib", "comdlg32.lib", "wsock32.lib", "user32.lib", "oleaut32.lib"]
  101.     for moddefn in moddefns:
  102.         print "# Module", moddefn.name
  103.         for file in moddefn.sourceFiles:
  104.             base = os.path.basename(file)
  105.             base, ext = os.path.splitext(base)
  106.             objects.append(base + ".obj")
  107.             print '$(temp_dir)\%s.obj: "%s"' % (base, file)
  108.             print "\t@$(CC) -c -nologo /Fo$* $(cdl) $(c_debug) /D BUILD_FREEZE",
  109.             print "-I$(pythonhome)/Include  -I$(pythonhome)/PC \\"
  110.             print "\t\t$(cflags) $(cdebug) $(cinclude) \\"
  111.             extra = moddefn.GetCompilerOptions()
  112.             if extra:
  113.                 print "\t\t%s \\" % (string.join(extra),)
  114.             print '\t\t"%s"' % file
  115.             print
  116.  
  117.         # Add .lib files this module needs
  118.         for modlib in moddefn.GetLinkerLibs():
  119.             if modlib not in libs:
  120.                 libs.append(modlib)
  121.  
  122.     print "ADDN_LINK_FILES=",
  123.     for addn in vars['addn_link']: print '"%s"' % (addn),
  124.     print ; print
  125.  
  126.     print "OBJS=",
  127.     for obj in objects: print '"$(temp_dir)\%s"' % (obj),
  128.     print ; print
  129.  
  130.     print "LIBS=",
  131.     for lib in libs: print '"%s"' % (lib),
  132.     print ; print
  133.  
  134.     print "$(target)$(debug_suffix)%s: $(temp_dir) $(OBJS)" % (target_ext)
  135.     print "\tlink -out:$(target)$(debug_suffix)%s %s" % (target_ext, target_link_flags),
  136.     print "\t$(OBJS) \\"
  137.     print "\t$(LIBS) \\"
  138.     print "\t$(ADDN_LINK_FILES) \\"
  139.     print "\t$(pythonlib) $(lcustom) $(l_debug)\\"
  140.     print "\t$(resources)"
  141.     print
  142.     print "clean:"
  143.     print "\t-rm -f *.obj"
  144.     print "\t-rm -f $(target).exe"
  145.